package com.dz.tool.Service.Impl;

import com.dz.tool.Service.HibernateServer;
import com.dz.tool.Utils.XMLUtils;
import com.dz.tool.Vo.HibernateInfo;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;

@Service
public class HibernateServerImpl implements HibernateServer {

    @Override
    public void HibernateZHSelectSQL() {
        String filrPath = "E:\\再保\\reinsurance-mvc\\src\\main\\resources\\hibernate";

        getFiles(filrPath);
        System.out.println("文件数量：" + fileNames.size());

        List<String> filePathEND = new ArrayList<>();
        if (fileNames.size()>0) {
            for (String fileName : fileNames) {
                int i = fileName.indexOf(".hbm.xml");
                if (i>0) {
                    filePathEND.add(fileName);
                }
            }
        }

        System.out.println("过滤后文件数量："+ filePathEND.size());
        if (filePathEND.size()>0) {
            int h = 1;
            for (String path : filePathEND) {
                System.out.println("----------------------");
                System.out.println("当前：" + h++ + "/" + filePathEND.size());
                Map<String, String> map = XmlAnalysis(path);
                System.out.println(map.get("select"));
                System.out.println(map.get("insert"));
                System.out.println(map.get("update"));
                System.out.println("----------------------");
            }
        }

    }

    // 递归获取某路径下的所有文件，文件夹，并输出
    List<String> fileNames = new ArrayList<>();
    public void getFiles(String path) {
        File file = new File(path);
        // 如果这个路径是文件夹
        if (file.isDirectory()) {
            // 获取路径下的所有文件
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                // 如果还是文件夹 递归获取里面的文件 文件夹
                if (files[i].isDirectory()) {
                    System.out.println("目录：" + files[i].getPath());
                    fileNames.add(files[i].getPath());
                    getFiles(files[i].getPath());
                } else {
                    System.out.println("文件：" + files[i].getPath());
                    fileNames.add(files[i].getPath());
                }
            }
        } else {
            System.out.println("文件：" + file.getPath());
            fileNames.add(file.getPath());
        }
    }

    public Map<String,String> XmlAnalysis(String filePath){
        List<String> tableNameFromHibernateXML = null;
        List<HibernateInfo> hibernateCache = null;
        Map<String,String> endMap = new HashMap<>();

        try {
            tableNameFromHibernateXML = XMLUtils.getTableNameFromHibernateXML(filePath);
            hibernateCache = XMLUtils.getHibernateCache(filePath);

            System.out.println(tableNameFromHibernateXML.get(1));
            System.out.println(tableNameFromHibernateXML.get(2));

            String vosOne = "";
            String vosTwo = "";

            String sqlStr = "select ";
            for (HibernateInfo hibernateInfo : hibernateCache) {
                vosOne = vosOne + "," + hibernateInfo.getTableFiledName() +" as \"" + hibernateInfo.getFieldName() + "\"";
            }
            vosOne = vosOne.substring(1,vosOne.length());
            sqlStr = sqlStr + vosOne;
            sqlStr = sqlStr + " from " + tableNameFromHibernateXML.get(1);


            vosOne = "";
            vosTwo = "";
            String sqlStrInsert = "insert into " + tableNameFromHibernateXML.get(1);
            for (HibernateInfo hibernateInfo : hibernateCache) {
                vosOne = vosOne + "," + hibernateInfo.getTableFiledName();
                vosTwo = vosTwo + ",#{" + hibernateInfo.getFieldName() + "}";
            }
            vosOne = vosOne.substring(1,vosOne.length());
            vosTwo = vosTwo.substring(1,vosTwo.length());
            vosOne = " (" + vosOne + ")";
            vosTwo = " (" + vosTwo + ")";
            sqlStrInsert = sqlStrInsert + vosOne + " values " + vosTwo;


            vosOne = "";
            vosTwo = "";
            String sqlStrUpdate = "update " + tableNameFromHibernateXML.get(1) + " set ";
            for (HibernateInfo hibernateInfo : hibernateCache) {
                vosOne = vosOne + "," + hibernateInfo.getTableFiledName() + "=" + "#{" + hibernateInfo.getFieldName() + "}";
            }
            vosOne = vosOne.substring(1,vosOne.length());
            sqlStrUpdate = sqlStrUpdate + vosOne;

            endMap.put("select",sqlStr);
            endMap.put("insert",sqlStrInsert);
            endMap.put("update",sqlStrUpdate);
        }catch (Exception e){
            e.printStackTrace();
        }

        return endMap;
    }


}
